package com.zxc.Util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class windWeather {
    public String getWeatherApi() {
        String param = "key=2455b64ef81341b6872474172f7d4036&location=auto_ip";
        StringBuilder sb = new StringBuilder();
        InputStream is = null;
        BufferedReader br = null;
        PrintWriter out = null;
        try {
            //接口地址
            String url = "https://free-api.heweather.net/s6/weather";
            URL    uri = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
            connection.setRequestMethod("POST");
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(10000);
            connection.setRequestProperty("accept", "*/*");
            //发送参数
            connection.setDoOutput(true);
            out = new PrintWriter(connection.getOutputStream());
            out.print(param);
            out.flush();
            //接收结果
            is = connection.getInputStream();
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line;
            //缓冲逐行读取
            while ( ( line = br.readLine() ) != null ) {
                sb.append(line);
            }
//            System.out.println(sb.toString());
        } catch ( Exception ignored ) {
        } finally {
            //关闭流
            try {
                if(is!=null){
                    is.close();
                }
                if(br!=null){
                    br.close();
                }
                if (out!=null){
                    out.close();
                }
            } catch ( Exception ignored ) {}
        }
        return sb.toString();
    }
    public int getCondCode(Double condCode) {
//                    天气
            if(condCode>205&&condCode<213){
                return 3;
            }else if(condCode>30&&condCode<304){
                return 2;
            }else if(condCode>306){
                return 1;
            }else {
                return 0;
            }
    }
    public int getTmpMax(Double tmpMax) {
//                    气温
        if(tmpMax>36){
            return 2;
        }else if(tmpMax>33){
            return 1;
        }else if(tmpMax<0){
            return -1;
        }else {
            return 0;
        }
    }
    public int getPcpn(Double pcpn) {
//                    降水
        if(pcpn>25){
            return 1;
        }else {
            return 0;
        }
    }
    public int getWindSc(Double windSpd) {
//                    风速
        if(windSpd>25){
            return 1;
        }else {
            return 0;
        }
    }

}
